<?php
class UserInformation extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->helper(array('url','form','security'));
		$this->load->helper('date');	
	}

	function index()
	{	
		
		$this->form_validation->set_rules('fname', 'Förnamn', 'required');
		$this->form_validation->set_rules('ename', 'Efternamn', 'required');
		$this->form_validation->set_rules('pnum', 'Personnummer', 'required|min_length[10]|max_length[10]');
		$this->form_validation->set_rules('adress', 'Adress', 'required');
		$this->form_validation->set_rules('telNumber', 'Telefonnummer', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		
		
	
		$userID = $this->session->userdata('UserId');
		
		if($userID == TRUE)
		{
				
				$data['title']='Dina bokningar';
				$data['online']=''; 
								
				
				$userInformation = $this->_getUserInfo($userID);
				
				$data['userInformation']= $userInformation;
				$data['label']='Din kontaktinformation';
				
			
				$this->load->view('global/view_master_header',$data);
				$this->load->view('user/view_userinformation', $data);
				$this->load->view('global/view_master_footer');	
			
			
			
		}
		
		else
		{		
				$data['label']='';
				$data['message']='Du måste logga in för att visa sidan.';
				$data['link']=anchor('user/login','Logga in'); 
				$this->load->view('global/view_master_header',$data);
				$this->load->view('global/view_redirect', $data);
				$this->load->view('global/view_master_footer');	
		}
		
		
		
	}
			
	
	
	private function _getUserInfo($id)
	{
		
		$this->load->model('userinformation_model');
		$userInformation = $this->userinformation_model->getUserInformation($id);
		
		if($userInformation != NULL)
		{
			return $userInformation;
		}
		
		else
		{
			echo "Hittade inte informationen, kolla så du är inloggad";
		}
		
	}
	
	
	function checkIfInfoChanged()
	{
		//kollar så att användaren är inloggad
		if($this->session->userdata('UserId'))
		{	
			$this->load->model('userinformation_model');
			
			$userID = $this->session->userdata('UserId');
			$oldInfo = $this->getUserInfo($userID);
			
			$newFirstName=trim($this->input->post('fname'));
			$newLastName=trim($this->input->post('lname'));
			$newPersonNumber=trim($this->input->post('pnum'));
			$newAdress=trim($this->input->post('adress'));
			$newTelNumber=trim($this->input->post('telNumber'));
			$newEmail=trim($this->input->post('email'));
			
			$dataChanged = FALSE;
			
			if ($newFirstName != $oldInfo->Fnamn)
			{
				$result = $this->userinformation_model->saveNewFname($userID, $newFirstName);
				
				$dataChanged = TRUE;
			}
			
			if ($newLastName != $oldInfo->Enamn)
			{
				$result = $this->userinformation_model->saveNewLname($userID, $newLastName);
				
				$dataChanged = TRUE;
			}
			
			if ($newPersonNumber != $oldInfo->Personnummer)
			{
				$result = $this->userinformation_model->saveNewPnumber($userID, $newPersonNumber);
				
				$dataChanged = TRUE;
			}
			
			if ($newAdress != $oldInfo->Adress)
			{
				$result = $this->userinformation_model->saveNewAdress($userID, $newAdress);
				
				$dataChanged = TRUE;
			}
			
			if ($newTelNumber != $oldInfo->TelNummer)
			{
				$result = $this->userinformation_model->saveNewTelNumber($userID, $newTelNumber);
				
				$dataChanged = TRUE;
			}
			
			if ($newEmail != $oldInfo->Email)
			{
				$result = $this->userinformation_model->saveNewEmail($userID, $newEmail);
				
				$dataChanged = TRUE;
			}
			
			
			if($dataChanged == TRUE)
			{
					
					$data['title']='Ändrad info';
					$data['message']='Nya informationen är sparad.'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_user_mypages_menu');
					$this->load->view('global/view_redirect', $data);
					$this->load->view('global/view_master_footer');	
					
			}
				
			else
			{
					$data['title']='Ändrad info';
					$data['message']='Ingen ny information sparad'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_user_mypages_menu');
					$this->load->view('global/view_redirect', $data);
					$this->load->view('global/view_master_footer');	
			}
		}
		else
		{
				//kod som körs om inte användaren är inloggad.
				$data['message']='Du måste logga in för att visa sidan.';
				$data['link']=anchor('user/login','Logga in'); 
				$this->load->view('global/view_master_header',$data);
				$this->load->view('global/view_redirect', $data);
				$this->load->view('global/view_master_footer');
		}
	}
	

	function changePassword()
	{
		//kontrollerar att användaren är inloggad.
		if($this->session->userdata('UserId'))
		{
			//Kod som körs om användaren är inloggad.
			$data['title']='Nytt lösenord';
			$data['ID'] = $this->session->userdata('UserId');
			$this->load->view('global/view_master_header',$data);
			$this->load->view('user/view_user_changePassword', $data);
			$this->load->view('global/view_master_footer');
		}
		else
		{
			//kod som körs om användaren inte är inloggad.
			$data['message']='Du måste logga in för att visa sidan.';
			$data['link']=anchor('user/login','Logga in'); 
			$this->load->view('global/view_master_header',$data);
			$this->load->view('global/view_redirect', $data);
			$this->load->view('global/view_master_footer');
		}	
	}
	
	
	function savePassword()
	{
		//Kontollerar att användaren är inloggad
		if($this->session->userdata('UserId'))
		{
			$this->load->model('userinformation_model');
			$id=trim($this->input->post('txtID'));
			$oldPassword=trim($this->input->post('oldPassword'));
			$newPassword=trim($this->input->post('newPassword'));
			$newPasswordConfirm=trim($this->input->post('newPasswordConfirm'));
			
			
			if ($newPassword == $newPasswordConfirm)
			{
				$result = $this->userinformation_model->saveNewPassword($id, $newPassword);
				if($result==TRUE)
				{
					$data['title']='Ändrat lösenord';
					$data['message']='Nya lösenordet är sparat.'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_user_mypages_menu');
					$this->load->view('global/view_redirect', $data);
					$this->load->view('global/view_master_footer');	
				}
				else
				{
					$data['title']='Ändrat lösenord';
					$data['message']='Lösenordet kunde inte sparas.'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_user_mypages_menu');
					$this->load->view('global/view_redirect', $data);
					$this->load->view('global/view_master_footer');
					
				}
			}
			
			else
			{
				$data['title']='Nytt lösenord';
				$data['error']='Det nya lösenordet och konfirmations lösenordet stämmer inte med varandra';
				$data['ID'] = $this->session->userdata('UserId');
				$this->load->view('global/view_master_header',$data);
				$this->load->view('user/view_user_changePassword', $data);
				$this->load->view('global/view_master_footer');
			}
		}
		else
		{
			//kod som körs om användaren inte är inloggad.
			$data['message']='Du måste logga in för att visa sidan.';
			$data['link']=anchor('user/login','Logga in'); 
			$this->load->view('global/view_master_header',$data);
			$this->load->view('global/view_redirect', $data);
			$this->load->view('global/view_master_footer');
		}
		
	}
}

?>